package com.petmall.controller;

import com.petmall.entity.Admin;
import com.petmall.entity.User;
import com.petmall.service.AdminService;
import com.petmall.util.SessionAttributes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpSession;

/**
 * Created by pig2guang on 2017/5/03.
 */
@Controller
public class AdminController {

    @Autowired
    private AdminService adminservice;

    @RequestMapping(value = "/adminLogin")
    public String adminLogin(@ModelAttribute(value = "admin") Admin adm, HttpSession httpSession, Model model ){
        Admin admin = adminservice.loginCheck(adm);
        if(admin != null){
            httpSession.setAttribute(new SessionAttributes().admin, admin.getAdmin_id());
            String login_time = adminservice.setLoginTime(admin.getAdmin_id());
            model.addAttribute("admin", admin);
            model.addAttribute("login_time", login_time);
            return "admin-management-center";
        }else{
            model.addAttribute("admin", new Admin());
            model.addAttribute("user", new User());
            return "login";
        }
    }

    @RequestMapping(value = "/adminManagement")
    public String adminLogin(HttpSession httpSession, Model model ){
        if(httpSession.getAttribute(new SessionAttributes().admin) != null){
            Admin admin = adminservice.getAdmin(httpSession.getAttribute(new SessionAttributes().admin));
            model.addAttribute("admin", admin);
            model.addAttribute("login_time", admin.getUpdate_time());
            return "admin-management-center";
        }else{
            model.addAttribute("admin", new Admin());
            model.addAttribute("user", new User());
            return "login";
        }
    }

}
